package com.xinxuan.contentcenter.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.nacos.common.utils.StringUtils;
import com.github.pagehelper.PageInfo;
import com.xinxuan.contentcenter.auth.CheckLogin;
import com.xinxuan.contentcenter.domain.Share;
import com.xinxuan.contentcenter.dto.ShareDTO;
import com.xinxuan.contentcenter.dto.ShareRequestDTO;
import com.xinxuan.contentcenter.service.ShareService;
import com.xinxuan.contentcenter.util.JwtOperator;

import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;

/**
 * @author xinsl
 * @date 2021/11/10
 */
@Slf4j
@RestController
@RequestMapping("/shares")
public class ShareController {

    @Autowired
    private ShareService shareService;

    @Autowired
    private JwtOperator jwtOperator;

    @GetMapping("/{id}")
    @CheckLogin
    // @SentinelResource(value = "test-sentinel-api"/*, blockHandlerClass = SentinelControllerBlokHandlerClass.class,
    // fallbackClass = SentinelControllerFallBackHandlerClass.class*/)
    public ShareDTO findById(@PathVariable Integer id) {
        if (StringUtils.isBlank(id.toString())) {
            throw new IllegalArgumentException("id不能为空");
        }
        return shareService.findById(id);
    }

    @GetMapping("/preview/{id}")
    @CheckLogin
    // @SentinelResource(value = "test-sentinel-api"/*, blockHandlerClass = SentinelControllerBlokHandlerClass.class,
    // fallbackClass = SentinelControllerFallBackHandlerClass.class*/)
    public ShareDTO getShare(@PathVariable Integer id) {
        if (StringUtils.isBlank(id.toString())) {
            throw new IllegalArgumentException("id不能为空");
        }
        return shareService.findById(id);
    }

    @GetMapping("/q")
    public PageInfo<Share> q(@RequestParam(required = false) String title,
        @RequestParam(required = false, defaultValue = "1") Integer pageNo,
        @RequestParam(required = false, defaultValue = "10") Integer pageSize,
        @RequestHeader(value = "X-Token", required = false) String token) {
        Integer userId = null;
        if (!StringUtils.equals(token, "[object Undefined]")) {
            Claims claims = jwtOperator.getClaimsFromToken(token);
            userId = (Integer)claims.get("id");
        }

        return shareService.q(title, pageNo, pageSize, userId);
    }

    @GetMapping("/exchange/{id}")
    @CheckLogin
    // @SentinelResource(value = "test-sentinel-api"/*, blockHandlerClass = SentinelControllerBlokHandlerClass.class,
    // fallbackClass = SentinelControllerFallBackHandlerClass.class*/)
    public Share exchangeById(@PathVariable Integer id, HttpServletRequest request) {
        return shareService.exchangeById(id, request);
    }

    @PostMapping("/contribute")
    @CheckLogin
    public Share contribute(@RequestBody ShareRequestDTO shareRequestDTO, @RequestHeader("X-Token") String token) {
        return shareService.contribute(shareRequestDTO, token);
    }

    @PutMapping("/contribute/{id}")
    @CheckLogin
    public Share contributeUpd(@PathVariable("id") String id, @RequestBody ShareRequestDTO shareRequestDTO,
        @RequestHeader("X-Token") String token) {
        return shareService.contributeUpd(id, shareRequestDTO, token);
    }

    @GetMapping("/user")
    @CheckLogin
    public List<Share> getShares(@RequestHeader("X-Token") String token) {
        return shareService.getShares(token);
    }

    @GetMapping("/contributions")
    @CheckLogin
    public List<Share> getcontributions(@RequestHeader("X-Token") String token) {
        return shareService.getcontributions(token);
    }
}
